package com.qo.qsdk.ui.base.floater;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Anmi on 2018/4/16.
 */

public class FloatMenuView extends View {
    public static final int STATUS_LEFT = 3;//展开左边菜单
    public static final int STATUS_RIGHT = 4;//展开右边菜单

    private int mStatus = STATUS_RIGHT;//默认右边

    private Paint mPaint;//画笔
    private int mBackgroundColor = 0x00FFFFFF;//默认背景颜色 完全透明的白色

    private int mMenuBackgroundColor = -1;//菜单的背景颜色

    private RectF mBgRect;//菜单的背景矩阵
    private int mItemWidth = dip2px(48);//菜单项的宽度
    private int mItemHeight = dip2px(48);//菜单项的高度
    private int mItemLeft = dip2px(6);//菜单项左边的默认偏移值，这里是0
    private int mCorner = dip2px(2);//菜单背景的圆角多出的宽度


    private int mRadius = dip2px(4);//红点消息半径
    private final int mRedPointRadiuWithNoNum = dip2px(3);//红点圆半径

    private int mFontSizePointNum = sp2px(10);//红点消息数字的文字大小

    private int mFontSizeTitle = sp2px(10);//菜单项的title的文字大小
    private float mFirstItemTop;//菜单项的最小y值，上面起始那条线
    private boolean mDrawNum = false;//是否绘制数字，false则只绘制红点
    private boolean cicleBg = false;//菜单项背景是否绘制成圆形，false则绘制矩阵

    private List<FloatItem> mItemList = new ArrayList<>();//菜单项的内容
    private List<RectF> mItemRectList = new ArrayList<>();//菜单项所占用位置的记录，用于判断点击事件

    private OnMenuClickListener mOnMenuClickListener;//菜单项的点击事件回调

    private ObjectAnimator mAlphaAnim;//消失关闭动画的透明值

    //设置菜单内容集合
    public void setItemList(List<FloatItem> itemList) {
        mItemList = itemList;
    }

    //设置是否绘制红点数字
    public void drawNum(boolean drawNum) {
        mDrawNum = drawNum;
    }

    //设置是否绘制圆形菜单，否则矩阵
    public void setCicleBg(boolean cicleBg) {
        this.cicleBg = cicleBg;
    }

    //用于标记所依赖的view的screen的坐标，实际view的坐标是window坐标，所以这里后面会减去状态栏的高度


    //设置菜单的背景颜色
    public void setMenuBackgroundColor(int mMenuBackgroundColor) {
        this.mMenuBackgroundColor = mMenuBackgroundColor;
    }

    //设置这个view（整个屏幕）的背景，这里默认透明
    public void setBackgroundColor(int BackgroundColor) {
        this.mBackgroundColor = BackgroundColor;
    }


    //下面开始的注释我写不动了，看不懂的话请自行领悟吧
    public FloatMenuView(Context context) {
        super(context);
    }

    public FloatMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FloatMenuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public FloatMenuView(Context baseContext,  int status) {
        super(baseContext);
        mStatus = status;
        int screenWidth = getResources().getDisplayMetrics().widthPixels;
        int screenHeight = getResources().getDisplayMetrics().heightPixels;
        mBgRect = new RectF(0, 0, screenWidth, screenHeight);
        initView();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mItemWidth * mItemList.size() + mItemLeft + mItemWidth / 4, mItemHeight);
    }

    private void initView( ) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(sp2px(12));

        mAlphaAnim = ObjectAnimator.ofFloat(this, "alpha", 1.0f, 0f);
        mAlphaAnim.setDuration(50);
        mAlphaAnim.addListener(new MyAnimListener() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (mOnMenuClickListener != null) {
//                    removeView();
                    mOnMenuClickListener.dismiss();
                }
            }
        });

        mFirstItemTop = 0;
        if (mStatus == STATUS_LEFT) {
            mItemLeft = dip2px(6);
        } else {
            mItemLeft = dip2px(6);
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (mStatus) {
            case STATUS_LEFT:
                drawBackground(canvas);
                drawFloatLeftItem(canvas);
                break;
            case STATUS_RIGHT:
                drawBackground(canvas);
                drawFloatLeftItem(canvas);

                break;
        }
    }

    private void drawBackground(Canvas canvas) {
        mPaint.setColor(mBackgroundColor);
        canvas.drawRect(mBgRect, mPaint);

    }

    private void drawFloatLeftItem(Canvas canvas) {
        mItemRectList.clear();
        if (mStatus == STATUS_RIGHT) {
            if (!FloatLogoMenu.isReverse) {
                FloatLogoMenu.isReverse = true;
//                Collections.reverse(mItemList);
            }
        }else{
            if (FloatLogoMenu.isReverse) {
                FloatLogoMenu.isReverse = false;
//                Collections.reverse(mItemList);
            }
        }
        float x = mItemLeft;
        for (int i = 0; i < mItemList.size(); i++) {
            canvas.save();
            mPaint.setColor(mMenuBackgroundColor);

            float right = (float) (x + mItemWidth);

//            if (cicleBg) {
//                float cx = left + mItemWidth / 4;//x中心点
//                float cy = mFirstItemTop + mItemHeight / 2;//y中心点
//                float radius = mItemWidth / 2;//半径
//                canvas.drawCircle(cx, cy, radius, mPaint);
//                mPaint.setColor(Color.parseColor("#ff0000"));
//                canvas.drawCircle(cx, cy, radius, mPaint);
//            } else {
//                mPaint.setColor(i%2==0?Color.parseColor("#ff0000"):Color.parseColor("#00ff00"));
//                canvas.drawRect(x, mFirstItemTop, right, mFirstItemTop + mItemHeight, mPaint);
//            }

            mItemRectList.add(new RectF(x, mFirstItemTop, right, mFirstItemTop + mItemHeight));
//            mPaint.setColor(i == 0 ? Color.parseColor("#80ff0000") : i == 1 ? Color.parseColor("#8000ff00") : Color.parseColor("#800000ff"));
//            canvas.drawRect(left, mFirstItemTop, right, mFirstItemTop + mItemHeight, mPaint);
            mPaint.setColor(mItemList.get(i).bgColor);
            drawIconTitleDot(canvas, i, x, right);
            x += mItemWidth;
        }
        canvas.restore();
    }


    private void drawIconTitleDot(Canvas canvas, int position, float l, float r) {
        FloatItem floatItem = mItemList.get(position);

        int w = floatItem.getW();// mItemWidth;
        int h = floatItem.getH();
        if (floatItem.icon != null) {
            float left = l + (r - l  - w)/2;//mItemLeft * (position + 1) + w * position;//计算每一个item的中心点x的坐标值
            float centryY = mItemHeight / 2;//计算每一个item的中心点的y坐标值

            float right = r - (r - l  - w)/2;//left + w;///2;

            float iconH = floatItem.getH();// mItemHeight * 0.5f;//计算出icon的宽度 = icon的高度

            mPaint.setColor(floatItem.titleColor);
            mPaint.setTextSize(mFontSizeTitle);
            float textH = getTextHeight(floatItem.getTitle(), mPaint);
            float textW = getTextWidth(floatItem.getTitle(), mPaint);
            float paddH = dip2px(8);//(mItemHeight - iconH - textH - mRadius) / 2;//总高度减去文字的高度，减去icon高度，再除以2就是上下的间距剩余

            float top = centryY - (h + paddH + textH)/2;//计算icon的上坐标值
            float bottom = top + iconH;//剩下的高度空间用于画文字

            //画icon
            Paint p = new Paint();
            p.setColor(Color.parseColor("#ffffff"));
            canvas.drawRect(left, top, right, bottom, p);
            canvas.drawBitmap(floatItem.icon, null, new RectF(left, top, right, bottom), new Paint());
            if (!TextUtils.isEmpty(floatItem.dotNum) && !floatItem.dotNum.equals("0")) {
                float dotLeft = left + w / 5;
                float cx = dotLeft + mCorner;//x中心点
                float cy = top + mCorner;//y中心点

                int radiu = mDrawNum ? mRadius : mRedPointRadiuWithNoNum;
                //画红点
                mPaint.setColor(Color.RED);
                canvas.drawCircle(cx, cy, radiu, mPaint);
                if (mDrawNum) {
                    mPaint.setColor(Color.WHITE);
                    mPaint.setTextSize(mFontSizePointNum);
                    //画红点消息数
                    canvas.drawText(floatItem.dotNum, cx - getTextWidth(floatItem.getDotNum(), mPaint) / 2, cy + getTextHeight(floatItem.getDotNum(), mPaint) / 2, mPaint);
                }
            }
            //画menu title
            float textX = left + (right - left) / 2 - textW / 2;
            float textY = bottom + textH / 2 + paddH;
            canvas.drawText(floatItem.title, textX, textY, mPaint);
        }
    }


    public void startAnim() {
        if (mItemList.size() == 0) {
            return;
        }
        invalidate();
    }


    private void removeView() {
        ViewGroup vg = (ViewGroup) this.getParent();
        if (vg != null) {
            vg.removeView(this);
        }
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        if (visibility == GONE) {
            if (mOnMenuClickListener != null) {
                mOnMenuClickListener.dismiss();
            }
        }
        super.onWindowVisibilityChanged(visibility);


    }

    public void setOnMenuClickListener(OnMenuClickListener onMenuClickListener) {
        this.mOnMenuClickListener = onMenuClickListener;
    }

    public interface OnMenuClickListener {
        void onItemClick(int position, String title);

        void dismiss();

    }

    private abstract class MyAnimListener implements Animator.AnimatorListener {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                for (int i = 0; i < mItemRectList.size(); i++) {
                    if (mOnMenuClickListener != null && isPointInRect(new PointF(event.getX(), event.getY()), mItemRectList.get(i))) {
                        mOnMenuClickListener.onItemClick(i, mItemList.get(i).title);
                        return true;
                    }
                }
                return true;
        }
        return false;
    }

    private boolean isPointInRect(PointF pointF, RectF targetRect) {
        return pointF.x >= targetRect.left && pointF.x <= targetRect.right && pointF.y >= targetRect.top && pointF.y <= targetRect.bottom;
    }


    public static class Builder {

        private Context mActivity;
        private List<FloatItem> mFloatItems = new ArrayList<>();
        private int mBgColor = Color.TRANSPARENT;
        private int mStatus = STATUS_LEFT;
        private boolean cicleBg = false;
        private int mMenuBackgroundColor = -1;
        private boolean mDrawNum = false;


        public Builder drawNum(boolean drawNum) {
            mDrawNum = drawNum;
            return this;
        }


        public Builder setMenuBackgroundColor(int mMenuBackgroundColor) {
            this.mMenuBackgroundColor = mMenuBackgroundColor;
            return this;
        }


        public Builder setCicleBg(boolean cicleBg) {
            this.cicleBg = cicleBg;
            return this;
        }

        public Builder setStatus(int status) {
            mStatus = status;
            return this;
        }

        public Builder setFloatItems(List<FloatItem> floatItems) {
            this.mFloatItems = floatItems;
            return this;
        }


        public Builder(Context activity ) {
            mActivity = activity;
        }

        public Builder addItem(FloatItem floatItem) {
            mFloatItems.add(floatItem);
            return this;
        }

        public Builder addItems(List<FloatItem> list) {
            mFloatItems.addAll(list);
            return this;
        }

        public Builder setBackgroundColor(int color) {
            mBgColor = color;
            return this;
        }

        public FloatMenuView create() {
            FloatMenuView floatMenuView = new FloatMenuView(mActivity, mStatus);
            floatMenuView.setItemList(mFloatItems);
            floatMenuView.setBackgroundColor(mBgColor);
            floatMenuView.setCicleBg(cicleBg);
            floatMenuView.startAnim();
            floatMenuView.drawNum(mDrawNum);
            floatMenuView.setMenuBackgroundColor(mMenuBackgroundColor);
            return floatMenuView;
        }

    }


    private int dip2px(float dipValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    private int sp2px(float spValue) {
        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    private float getTextHeight(String text, Paint paint) {
        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        return rect.height() / 1.1f;
    }

    private float getTextWidth(String text, Paint paint) {
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        return bounds.width() / 1.0f;
    }
}
